//
//  XCShareMusicViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/15.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCShareMusicViewController.h"
#import "XCShareMusicTableViewCell.h"
#import "XCShareMusicMoidel.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "UIImageView+WebCache.h"
#import "OnlineAVplay.h"
#import "XCPlayViewController.h"
@interface XCShareMusicViewController ()<UITableViewDataSource, UITableViewDelegate, XCPlayingDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, copy) NSString *senderUrl;

@property (nonatomic, copy) NSString *picUrl;

@property (nonatomic, retain) UIImageView *shareMusicImageView;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableSingerName;


@end

@implementation XCShareMusicViewController

- (void)dealloc {
    
    _tableView.delegate=nil;
    _tableView.dataSource=nil;
    
    [_tableView release];
    [_dataSource release];
    [_senderUrl release];
    [_shareMusicImageView release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"共享音乐, 品味人生";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 4)];
    self.shareMusicImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, self.view.bounds.size.width, self.view.bounds.size.height / 4 + 100)];

    [view addSubview:self.shareMusicImageView];

    self.tableView.tableHeaderView = view;
    self.dataSource = [NSMutableArray arrayWithCapacity:0];

    [self.view addSubview:self.tableView];
    [_tableView release];
    [self shareMusicNetRequest];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];

}
- (void)shareMusicNetRequest {
    
    [XCAFNetTool GETNetWithUrl:[XCUrlManger shareMusicNetRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {

        for (NSDictionary *dic in [result[@"data"] objectForKey:@"songs"]) {
            XCShareMusicMoidel *shareMusicModel = [[XCShareMusicMoidel alloc] initWithDataSource:dic];
            if (shareMusicModel.auditionList.count != 0 && shareMusicModel.picUrl != nil) {
                [self.dataSource addObject:shareMusicModel];
            }
            [shareMusicModel release];
        }
        XCShareMusicMoidel *shareMusicModel = self.dataSource[0];
        self.senderUrl = shareMusicModel.picUrl;
        [self.shareMusicImageView sd_setImageWithURL:[NSURL URLWithString:self.senderUrl]placeholderImage:[UIImage imageNamed:@"Image-p"]];

        self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
        self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
        self.mutableSingerName = [NSMutableArray arrayWithCapacity:0];
        self.nameArray = [NSMutableArray arrayWithCapacity:0];
        
        for (XCShareMusicMoidel *shareMusicModel in self.dataSource) {
            [self.mutableArraySongName addObject:[shareMusicModel.auditionList[0] objectForKey:@"url"]];
            [self.mutableHeadIVPicurl addObject:shareMusicModel.picUrl];
            [self.mutableSingerName addObject:shareMusicModel.singerName];
            [self.nameArray addObject:shareMusicModel.name];
        }

        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.shareMusicModel = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.shareMusicImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.shareMusicImageView.transform = CGAffineTransformMakeScale(scale, scale);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableSingerName;
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    XCShareMusicMoidel *shareMusicModel = self.dataSource[indexPath.row];
    
    [[OnlineAVplay shareAVPlayer] playWithUrl:[shareMusicModel.auditionList[0] objectForKey:@"url"]];
    [[OnlineAVplay shareAVPlayer] play];
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];

}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
